package oj;
import java.util.*;

//前序遍历：给你二叉树的根节点 root ，返回它节点值的 前序 遍历。
public class Solution_144 {
    public static List<Integer> preorderTraversal(TreeNode root){
        List<Integer> ans = new ArrayList<>();
        if(root == null){
            return ans;
        }
        //前序： 根 左 右
        ans.add(root.val);
        //将左子树的全部结点尾插到根结点的后边
        ans.addAll(preorderTraversal(root.left));
        //同理，将右结点的全部结点尾插到排好序的左子树结点后边
        ans.addAll(preorderTraversal(root.right));
        return ans;
    }

    public static void main(String[] args) {
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(2);
        TreeNode n3 = new TreeNode(3);
        TreeNode n4 = new TreeNode(4);
        TreeNode n5 = new TreeNode(5);
        TreeNode n6 = new TreeNode(6);
        TreeNode n7 = new TreeNode(7);
        TreeNode n8 = new TreeNode(8);
        TreeNode n9 = new TreeNode(9);
        TreeNode n10 = new TreeNode(10);
        TreeNode n11 = new TreeNode(11);

        n1.left = n2;   n1.right = n3;
        n2.left = n4;   n2.right = n5;
        n3.left = null; n3.right = n6;
        n4.left = null; n4.right = null;
        n5.left = n7;   n5.right = n8;
        n6.left = n9;   n6.right = null;
        n9.left = n10;  n9.right = n11;

        Solution_144 solution_144 = new Solution_144();
        List<Integer> list = solution_144.preorderTraversal(n1);
        System.out.println(list);
    }


}
